var config = require('./config.js')
var message = require('../../component/message/message')

/**
 * 获取电影列表
 */
function fetchFilms(url, start, count, cb, fail_cb) {
  var that = this;
  message.hide.call(that);
  if (that.data.hasMore){
    wx.request({
      url: url,
      data:{
        city: config.city,
        start: start,
        count: config.count
      },
      method: 'GET',
      header: {
        "Content-Type": "application/json,application/json"
      },
      success:function(res){
        console.log(res.data);
        if (res.data.subjects === undefined || res.data.subjects.length ===0){
          that.setData({
            hasMore: false
          });
        }else{
          that.setData({
            films: that.data.films.concat(res.data.subjects),
            start: that.data.start + res.data.subjects.length,
            showLoading: false
          });
          console.log(that.data.start);
        }
        wx.stopPullDownRefresh();
        typeof cb == 'function' && cb(res.data);
      },
      fail: function(){
        that.setData({
          showLoading: false
        });
        message.show.call(that, {
          content: '网络开小差了',
          icon: 'offline',
          duration: 3000
        });
        wx.stopPullDownRefresh();
        typeof fail_cb == 'function' && fail_cb();
      }
    });
  }
}


module.exports = {
  fetchFilms: fetchFilms
}